package com.dycong.common.leetcode.sort;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BubbleSort {

    public static void main(String[] args) {
        BubbleSort bubbleSort = new BubbleSort();
        int[] ints = bubbleSort.sort(new int[]{1, 2, 5, 8, 6, 5, 9, 5, 55, 5});
        System.out.println(Arrays.toString(ints));
    }

    /**
     * 小爷的冒泡法
     *
     * @param nums
     * @return
     */
    public int[] sort(int[] nums) {
        if (nums.length == 0 || nums.length == 1) {
            return nums;
        }
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    int tmp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                }
            }
        }
        return nums;
    }


    public int[] sort_(int[] nums) {
        if (nums.length == 0 || nums.length == 1) {
            return nums;
        }

        for (int i = 0; i < nums.length; i++) {
            boolean isSorted = true;
            for (int j = 0; j < nums.length - 1; j++) {
                if (nums[j] > nums[j + 1]) {
                    int tmp = nums[j];
                    nums[j] = nums[j + 1];
                    nums[j + 1] = tmp;
                    isSorted = false;
                }
            }
            if (isSorted) {
                break;
            }
        }
        return nums;
    }


    public List<Integer> parse(int[] nums) {
        List<Integer> res = new ArrayList<>(nums.length);

        for (int i : nums) {
            res.add(i);
        }
        return res;
    }
}
